package android.slc.module.sys.ui.dict.type

import android.slc.appbase.ui.AppViewModel
import android.slc.appbase.ui.dialog.SlcFastDialogUtils
import android.slc.appbase.utils.AppIntentUtils
import android.slc.appdb.core.DbEntityMaster
import android.slc.appdb.sys.entity.CsDictType
import android.slc.appdb.utils.DictEnumUtils
import android.slc.appdb.sys.enums.EnableEnum
import android.slc.appdb.sys.enums.StateEnum
import android.slc.appdb.sys.service.CsDictDataMapper
import android.slc.appdb.sys.service.CsDictTypeMapper
import android.slc.box.commonlibrary.compat.SlcResourceUtils
import javafx.beans.property.SimpleBooleanProperty
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleObjectProperty
import javafx.beans.property.SimpleStringProperty
import org.apache.commons.lang3.StringUtils

class DictTypeEditVm : AppViewModel() {

    val isEditProperty = SimpleBooleanProperty()

    lateinit var csDictType: CsDictType

    val dictNameProperty = SimpleStringProperty()

    val dictTypeProperty = SimpleStringProperty()

    val dictStatusProperty = SimpleObjectProperty<Int>()

    val dictSortProperty = SimpleIntegerProperty()

    val dictRemarkProperty = SimpleStringProperty()

    val editDictTypeStatusProperty = SimpleObjectProperty<Int>()

    fun initVm() {
        this.csDictType = AppIntentUtils.getDataByBundle(intentScope.bundle) ?: CsDictType()
        this.dictNameProperty.value = csDictType.dictName
        this.dictTypeProperty.value = csDictType.dictType
        this.dictStatusProperty.value = csDictType._status
        this.dictSortProperty.value = csDictType.sort
        this.dictRemarkProperty.value = csDictType._remark
        this.editDictTypeStatusProperty.value = csDictType.editDictTypeStatus
        this.isEditProperty.value = this.csDictType.eId != null
    }

    fun getStatusList(): List<String> {
        return DictEnumUtils.getLabelList(StateEnum.values())
    }

    fun setStatus(status: Int) {
        dictStatusProperty.value = status
    }

    fun getEnableList(): List<String> {
        return DictEnumUtils.getLabelList(EnableEnum.values())
    }

    fun setEditDictTypeStatus(status:Int){
        editDictTypeStatusProperty.value = status
    }

    fun save() {
        if (StringUtils.isEmpty(this.dictNameProperty.value)
            || StringUtils.isEmpty(this.dictTypeProperty.value)
            || this.dictStatusProperty.value == null
        ) {
            SlcFastDialogUtils.showPrompt(SlcResourceUtils.getString("label_content_cannot_be_empty_blank"))
            return
        }
        val oldDictType = this.csDictType.dictType
        this.csDictType.dictName = this.dictNameProperty.value
        this.csDictType.dictType = this.dictTypeProperty.value
        this.csDictType._status = this.dictStatusProperty.value
        this.csDictType.sort = this.dictSortProperty.value
        this.csDictType._remark = this.dictRemarkProperty.value
        this.csDictType.editDictTypeStatus = this.editDictTypeStatusProperty.value
        try {
            DbEntityMaster.getService(CsDictTypeMapper::class.java).put(this.csDictType)
            DbEntityMaster.getService(CsDictDataMapper::class.java)
                .updateDateType(oldDictType, this.csDictType.dictType)
            finishAndRefreshList()
        } catch (e: RuntimeException) {
            SlcFastDialogUtils.showPrompt(e.message ?: SlcResourceUtils.getString("label_save_failed"))
        }
    }

    public override fun finish() {
        super.finish()
    }

    public override fun finishView() {
        super.finishView()
    }
}